package com.yylending.las.lending.quota.myquota;

import android.os.Parcel;
import android.os.Parcelable;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yylending.las.Global;
import com.yylending.las.lending.quota.initdata.InitDataModel;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by laixj on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class MyQuotaModel extends Model {
	private static final String TAG = MyQuotaModel.class.getSimpleName();

	public final static int METHOD_MY_QUOTA = 105;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		switch (functionalCode) {
			case METHOD_MY_QUOTA:
				return service.myQuota(Global.LOGIN_TOKEN, new EmptyEntity());
		}
		return null;
	}

	@Override
	public void onNext(Object entity) {
		CLog.i(TAG, "onNext: " + entity.toString());
		BaseResultEntity result = cast(entity);
		switch (result.requestCode) {
			case METHOD_MY_QUOTA:
				MyQuotaResult quotaResult = cast(entity);
				EventAgent.post(quotaResult);
				break;
		}
	}

	private interface Service {
		/**
		 * 我的额度接口
		 */
		@POST("server/loan/getLoan")
		Observable<MyQuotaResult> myQuota(@Header("token") String token, @Body EmptyEntity params);
	}

	static class MyQuotaResult extends BaseResultEntity<ApplyStatus> {
	}

	public static class ApplyStatus implements Parcelable{
		/**
		 * quota : 计算获得的额度
		 * loanAmount : 借款金额
		 * amountState 	   "10": 有在途借款申请（非云贷）; "20": 已有在途借款; "100": 未获取额的; "110": 额度获取中; "120": 不符合要求; "130": 低额度; "140": 高额度
		 * loanState   	   "200": 借款申请中; "210": 需要现调; "220": 需要远程面签; "230": 现调失败; "240": 远程面签失败; "250": 借款否决,
		 * promoteAmount   "300":"可提额; "310": 无提额; "320": 提额申请中
		 * msg
		 */
		/**
		 * 计算获得的额度
		 */
		public Quota quota;
		/**
		 * 借款金额
		 */
		public String loanAmount;
		/**
		 * 额度状态
		 */
		public String amountState;
		/**
		 * 借款状态
		 */
		public String loanState;
		/**
		 * 提额状态
		 */
		public String promoteAmount;

		/**
		 * ""： 不需要判断 "0": 没有额度 "1": 有线下额度
		 */
		public String offLineAmount = "";
		/**
		 * "Y": 直批 "N": 非直批
		 */
		public String isDirectApproval;

		public String msg;

		public int page;

		public InitDataModel.LoanThreshold loanLine;

		public ApplyStatus() {
		}

		@Override
		public String toString() {
			return "ApplyStatus{" +
					"quota=" + quota +
					", loanAmount='" + loanAmount + '\'' +
					", amountState='" + amountState + '\'' +
					", loanState='" + loanState + '\'' +
					", promoteAmount='" + promoteAmount + '\'' +
					", offLineAmount='" + offLineAmount + '\'' +
					", isDirectApproval='" + isDirectApproval + '\'' +
					", msg='" + msg + '\'' +
					", page=" + page +
					'}';
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeParcelable(this.quota, flags);
			dest.writeString(this.loanAmount);
			dest.writeString(this.amountState);
			dest.writeString(this.loanState);
			dest.writeString(this.promoteAmount);
			dest.writeString(this.offLineAmount);
			dest.writeString(this.isDirectApproval);
			dest.writeString(this.msg);
			dest.writeInt(this.page);
		}

		protected ApplyStatus(Parcel in) {
			this.quota = in.readParcelable(Quota.class.getClassLoader());
			this.loanAmount = in.readString();
			this.amountState = in.readString();
			this.loanState = in.readString();
			this.promoteAmount = in.readString();
			this.offLineAmount = in.readString();
			this.isDirectApproval = in.readString();
			this.msg = in.readString();
			this.page = in.readInt();
		}

		public static final Creator<ApplyStatus> CREATOR = new Creator<ApplyStatus>() {
			@Override
			public ApplyStatus createFromParcel(Parcel source) {
				return new ApplyStatus(source);
			}

			@Override
			public ApplyStatus[] newArray(int size) {
				return new ApplyStatus[size];
			}
		};

		@Override
		public boolean equals(Object o) {
			if (this == o)
				return true;
			if (o == null || getClass() != o.getClass())
				return false;

			ApplyStatus that = (ApplyStatus) o;

			if (quota != null ? !quota.equals(that.quota) : that.quota != null)
				return false;
			if (loanAmount != null ? !loanAmount.equals(that.loanAmount) : that.loanAmount != null)
				return false;
			if (amountState != null ? !amountState.equals(that.amountState) : that.amountState != null)
				return false;
			if (loanState != null ? !loanState.equals(that.loanState) : that.loanState != null)
				return false;
			if (promoteAmount != null ? !promoteAmount.equals(that.promoteAmount) : that.promoteAmount != null)
				return false;
			if (offLineAmount != null ? !offLineAmount.equals(that.offLineAmount) : that.offLineAmount != null)
				return false;
			return isDirectApproval != null ? isDirectApproval.equals(that.isDirectApproval) : that.isDirectApproval == null;

		}
	}

	public static class Quota implements Parcelable{
		/**
		 * 12期额度
		 */
		public String tl;
		/**
		 * 24期额度
		 */
		public String tf;
		/**
		 * 36期额度
		 */
		public String ts;

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(this.tl);
			dest.writeString(this.tf);
			dest.writeString(this.ts);
		}

		public Quota() {
		}

		protected Quota(Parcel in) {
			this.tl = in.readString();
			this.tf = in.readString();
			this.ts = in.readString();
		}

		public static final Creator<Quota> CREATOR = new Creator<Quota>() {
			@Override
			public Quota createFromParcel(Parcel source) {
				return new Quota(source);
			}

			@Override
			public Quota[] newArray(int size) {
				return new Quota[size];
			}
		};

		@Override
		public String toString() {
			return "Quota{" +
					"tl='" + tl + '\'' +
					", tf='" + tf + '\'' +
					", ts='" + ts + '\'' +
					'}';
		}

		@Override
		public boolean equals(Object o) {
			if (this == o)
				return true;
			if (o == null || getClass() != o.getClass())
				return false;

			Quota quota = (Quota) o;

			if (tl != null ? !tl.equals(quota.tl) : quota.tl != null)
				return false;
			if (tf != null ? !tf.equals(quota.tf) : quota.tf != null)
				return false;
			return ts != null ? ts.equals(quota.ts) : quota.ts == null;

		}
	}
}